package org.openzip.openzip.gui.util.checkBox;

import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JLabel;

import org.openzip.openzip.gui.util.FileUtil;

/**
 * An extension of the <code>ObserverThreestateCheckBoxModel</code> to provide
 * a checkbox with the file icon and the filename. The key part is to override
 * the getGraphicalRepresentation method to add the label with file icon
 * and file name to the returned Container.
 * 
 * @author Jakub Závěrka
 */
public class FileCheckBoxModel extends ObserverThreestateCheckBoxModel 
{	
	//underlying file
	private File file;
	//the file label
	private JLabel label;
	
	/**
	 * Construct a <code>FileCheckBoxModel</code> with the specified file.
	 * @param file
	 */
	public FileCheckBoxModel(File file) {
		super();
		this.file = file;
		
		//text is either the file name (for files) or absolute path (for roots - 
		//drives).
		String text = file.getName();
		if(text == null || text.equals("")){
			text = file.getAbsolutePath();
		}
		
		//get icon of the file
		Icon icon = FileUtil.getFileIcon(file);
		
		//constructs label with the text and icon
		label = new JLabel(text);
		if(icon != null){
			label.setIcon(icon);
		}
		//move the label by 16 pixels so it won't get painted over the checkbox.
		label.setBorder(BorderFactory.createEmptyBorder(0, 16, 0, 0));
	}
	
	/**
	 * Returns a checkbox with the file name and the file icon.
	 */
	@Override
	public ThreestateCheckBox getGraphicalRepresentation(){
		//get the original representation
		ThreestateCheckBox c = super.getGraphicalRepresentation();
		//add the label to the representation.
		c.removeAll();
		c.add(label);
		return c;
	}
			
	/**
	 * Returns the file associated with this checkbox.
	 */
	public File getFile() {
		return this.file;
	}
}
